/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap/bootstrap');

window.Vue = require('vue');

import router from './bootstrap/router'
import store from './store'
import iView from 'iview'
import Util from './libs/util'

Vue.use(iView)

/**
 * 自定义组件
 */
window.ModelDataSource = require('./libs/ModelDataSource');
Vue.use(ModelDataSource);
window.ListDataSource = require('./libs/ListDataSource');
Vue.use(ListDataSource);
//阻止 vue 在启动时生成生产提示。
Vue.config.productionTip = false

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */


/**
 * 路由全局钩子
 */
// router.beforeEach((to, from, next) => {
//   iView.LoadingBar.start();
//
//   //设置标题
//   store.commit('setTitle', to.meta.title);
//
//   if (to.name === 'login') {
//     if (Cookie.get('user')){
//       router.replace({
//         name: 'home_index'
//       });
//       next()
//     } else {
//       store.commit('setAuthRedirectUrl', from.path);
//       next();
//     }
//   } else if (to.path === '/') {
//     router.replace({
//       name: 'home_index'
//     });
//     next()
//   }
//
//   //验证登录
//   else if (to.meta.requiresAuth) {
//     if (Cookie.get('user')){
//       let user = JSON.parse(Cookie.get('user'));
//
//       //验证权限
//       if (to.meta.requirePermission
//         && user.permissions.findIndex(item => item === to.meta.requirePermission) === -1){
//         router.replace({
//           name: 'error_403'
//         });
//         next()
//       } else {
//         store.commit('openNewPage', to);
//         store.commit('setBreadCrumbsByRoute', to);
//         next();
//       }
//     }else{
//       next({
//         name: 'login'
//       })
//     }
//   } else {
//     next()
//   }
//
//   iView.LoadingBar.finish();
// });
//
// router.afterEach((to, from, next) => {
//   window.scrollTo(0, 0);
// });

/**
 * 全局组件
 */
Vue.component('pagination', require('./components/Pagination.vue'));
Vue.component('vue-table', require('./components/Table.vue'));
Vue.component('bs4-card', require('./components/card.vue'));
Vue.component('vue-tree-table', require('./components/TreeTable.vue'));
Vue.component('app', require('./app.vue'));

const app = new Vue({
  el: '#app',
  router: router,
  store: store,
  template: '<app/>',
  mounted () {
    // if (Cookie.get('user')) {
    //   this.$store.commit('setUser', JSON.parse(Cookie.get('user')));
    // }
  }
});

/**
 * Handling Errors
 */
window.axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response;
}, function (error) {
  let response = error.response;
  console.log('Error', response);
  if (response){
    switch (response.status) {
      case 401:
        Util.logout()
        break;
      case 422:
        if (response.data.errors){
          _.forOwn(response.data.errors, (item, key) => {
            app.$Notice.error({
            title: '数据不合法',
            desc: item[0]
          })
        })
        } else {
          _.forOwn(response.data, (item, key) => {
            app.$Notice.error({
            title: '数据不合法',
            desc: item
          })
        })
        }
        break;
      case 500:
        app.$Notice.error({
          title: '程序错误',
          desc: '请联系技术人员'
        });
        break;
      default:
        break;
    }
  } else {
    app.$Notice.error({
      title: '数据加载超时',
      desc: '请尝试刷新页面，并仔细检查网络链接状况或联系技术人员！'
    });
  }

  return Promise.reject(error);
});
